<template>
  <view class="index">
    <view class="jtop">
      <view class="status_bar"><!-- 这里是状态栏 --></view>
      <!-- <view class="jtop_l" @tap="navigateBack"><image src="../../static/img/ic_back.png"></image></view> -->
      <view class="jtop_l" @tap="navigateBack">取消</view>
      <view class="jtop_c">新增机具领用</view>
      <view class="jtop_r" :style="{ opacity: name ? '1' : '.6' }" @tap="jsave"
        >确定</view
      >
    </view>
    <view class="tophr"></view>

    <!-- <view class="addb">
			<view class="addb_1">
				2020年6月27日    星期一
			</view>
		</view> -->
    <view class="addb">
      <view class="addi2">
        <text>机具名称</text>
        <input
          type="text"
          placeholder="请输入机具名称"
          :value="name"
          @input="onKeyInput_o"
          placeholder-style="color:#CCCCCC"
        />
      </view>
      <view class="addi2">
        <text>领用数量</text>
        <input
          type="text"
          placeholder="请输入领用数量"
          :value="amount"
          @input="onKeyInput_t"
          placeholder-style="color:#CCCCCC"
        />
      </view>
      <view class="addi2">
        <text>领用人</text>
        <input
          type="text"
          placeholder="请输入领用人"
          :value="receiver"
          @input="onKeyInput_c"
          placeholder-style="color:#CCCCCC"
        />
      </view>
    </view>

    <view class="addb ">
      <view class="addi2 addi3">
        <text>签字确认</text>

        <canvas
          :class="[ks ? 'firstCanvas' : 'firstCanvas firstCanvas_nobj']"
          canvas-id="firstCanvas"
          @touchmove="move"
          @touchstart="start($event)"
          @touchend="end"
          @touchcancel="cancel"
          @longtap="tap"
          disable-scroll="true"
          @error="error"
        ></canvas>
      </view>

      <image src="../../static/img/qr.png" class="qr" @tap="overSign"></image>
      <image src="../../static/img/qk.png" class="qk" @tap="clearClick"></image>
    </view>
  </view>
</template>

<script>
import request from '@/common/request.js';

var content = null;
var touchs = [];
var canvasw = 0;
var canvash = 0;
var _that;
//获取系统信息
uni.getSystemInfo({
  success: function(res) {
    canvasw = res.windowWidth;
    canvash = res.windowHeight;
  },
});

import { mapState } from 'vuex';

export default {
  data() {
    return {
      repeat_click: true,
      ashowpop: false,
      ashowpop_jrename: false,
      ashowpop_jdel: false,
      ashowpopT: false,
      bj: false,
      yd: false,
      sj: true,

      ks: true,

      signImage: '',
      isEnd: false, // 是否签名，
      name: '',
      amount: '',
      receiver: '',
    };
  },

  methods: {
    onKeyInput_o: function(event) {
      this.name = event.detail.value;
    },
    onKeyInput_t: function(event) {
      this.amount = event.detail.value;
    },
    onKeyInput_c: function(event) {
      this.receiver = event.detail.value;
    },

    jsave() {
      if (!this.repeat_click) {
        return;
      }
      this.repeat_click = false;

      // if(!this.name){

      // 						this.repeat_click = true;
      //	uni.showToast({
      // 		title: '请输入使用名目',
      // 		icon: 'none'
      // 	})
      // 	return
      // }

      // if(!this.amount){
      // 						this.repeat_click = true;
      //					uni.showToast({
      // 		title: '请输入使用数量',
      // 		icon: 'none'
      // 	})
      // 	return
      // }

      if (!this.signImage) {
        this.repeat_click = true;
        uni.showToast({
          title: '记得签名哦，签好后记得点击打钩 :-)',
          icon: 'none',
        });
        return;
      }

      var jd = {
        name: this.name,
        amount: this.amount,
        receiver: this.receiver,
        materialId: this.tid,
        sign: this.signImage,
      };

      request({
        url: `/biz/material-out/borrow`,
        data: jd,

        method: 'POST',
        success: openIdRes => {
          console.log(openIdRes);
          if (openIdRes.data.success == true) {
            wx.showToast({
              title: '添加成功',
              icon: 'none',
              duration: 1000,
              success: function() {
                setTimeout(function() {
                  uni.navigateBack();
                }, 1500);
              },
            });

            // uni.navigateBack()
          } else {
            this.repeat_click = true;
            uni.showToast({
              title: openIdRes.data.msg,
              icon: 'none',
            });
            this.isLoading = false;
          }
        },
        fail: () => {
          this.repeat_click = true;
          uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none',
          });
          return false;
        },
      });
    },

    overSign: function() {
      if (this.isEnd) {
        uni.canvasToTempFilePath({
          canvasId: 'firstCanvas',

          success: function(res) {
            //打印图片路径
            console.log(res.tempFilePath);
            console.log('完成签名');
            //设置图片
            _that.signImage = res.tempFilePath;

            this.repeat_click = true;
            uni.showToast({
              title: '确认完成',
              icon: 'none',
              duration: 1500,
              mask: true,
            });
          },
        });
      } else {
        this.repeat_click = true;
        uni.showToast({
          title: '请先完成签名',
          icon: 'none',
          duration: 1500,
          mask: true,
        });
      }
    },

    // 画布的触摸移动开始手势响应
    start: function(event) {
      this.ks = false;
      // console.log(event)
      console.log('触摸开始' + event.changedTouches[0].x);
      console.log('触摸开始' + event.changedTouches[0].y);
      //获取触摸开始的 x,y
      let point = {
        x: event.changedTouches[0].x,
        y: event.changedTouches[0].y,
      };
      // console.log(point)
      touchs.push(point);
    },
    // 画布的触摸移动手势响应
    move: function(e) {
      let point = {
        x: e.touches[0].x,
        y: e.touches[0].y,
      };
      // console.log(point)
      touchs.push(point);
      if (touchs.length >= 2) {
        this.draw(touchs);
      }
    },

    // 画布的触摸移动结束手势响应
    end: function(e) {
      console.log('触摸结束' + e);
      // 设置为已经签名
      this.isEnd = true;
      // 清空轨迹数组
      for (let i = 0; i < touchs.length; i++) {
        touchs.pop();
      }
    },

    // 画布的触摸取消响应
    cancel: function(e) {
      console.log('触摸取消' + e);
    },

    // 画布的长按手势响应
    tap: function(e) {
      console.log('长按手势' + e);
    },

    error: function(e) {
      console.log('画布触摸错误' + e);
    },

    //绘制
    draw: function(touchs) {
      console.log(touchs[0], touchs[1]);
      let point1 = touchs[0];
      let point2 = touchs[1];
      touchs.shift();
      content.moveTo(point1.x, point1.y);
      content.lineTo(point2.x, point2.y);
      content.stroke();
      content.draw(true);
    },
    //清除操作
    clearClick: function() {
      // 设置为未签名
      this.isEnd = false;
      //清除画布
      content.clearRect(0, 0, canvasw, canvash);
      content.draw(true);
    },

    // 跳转
    jshow_pop() {
      this.ashowpop = !this.ashowpop;
    },
    btTC2() {
      uni.navigateTo({
        url: '../main/work_add',
      });
    },

    bt_sj() {
      this.sj = !this.sj;
    },

    navigateBack() {
      uni.navigateBack();
    },
  },
  computed: mapState(['forcedLogin', 'hasLogin', 'userName']),
  onLoad(e) {
    console.log('111111:', e);
    this.tn = e.parameter;

    this.tid = e.id;
    this.tdw = e.dw;

    // this.handwriting = new Handwriting({
    //   lineColor: this.lineColor,
    //   slideValue: this.slideValue, // 0, 25, 50, 75, 100
    //   canvasName: 'handWriting',
    // })

    _that = this;
    //获得Canvas的上下文
    content = wx.createCanvasContext('firstCanvas');
    //设置线的颜色
    content.setStrokeStyle('#000');
    //设置线的宽度
    content.setLineWidth(5);
    //设置线两端端点样式更加圆润
    content.setLineCap('round');
    //设置两条线连接处更加圆润
    content.setLineJoin('round');
  },
};
</script>

<style>
.firstCanvas {
  width: 100%;
  height: 500rpx;
  display: block;
  float: left;
  background-color: #ffffff;
  background-image: url(../../static/img/bz.png);
  background-repeat: no-repeat;
  background-size: 236rpx 33rpx;
  background-position: center;
}
.firstCanvas_nobj {
  background-image: none;
}
.addi3 {
  height: auto !important;
}
.mm {
  width: 720rpx;
  margin: 0 0 0rpx 30rpx;
  height: auto;
  display: block;
  float: left;
}
.mm view {
  width: auto;
  height: 80rpx;
  margin: 0 50rpx 0 0rpx;
  display: block;
  float: left;
  overflow: hidden;
}
.mm view text {
  width: auto;
  height: auto;
  margin: 0rpx 0 0rpx 0rpx;
  height: auto;
  display: block;
  font-size: 28rpx;
  line-height: 36px;
  color: #666666;
}
.mm view i {
  width: 60rpx;
  height: 20rpx;
  margin: 4rpx auto 0rpx;
  display: block;
  background: rgb(242, 242, 242);
}
.mm view.active i {
  background: #256aff;
}
.mm view.active text {
  color: #256aff;
}

.tophr {
  width: 100%;
  height: 88rpx;
}
.uni-searchbar {
  box-shadow: none;
}

.at {
  width: 160rpx;
  height: 160rpx;
  display: block;
  float: left;
  margin: 0rpx 0 0 0;
}
.att {
  width: 720rpx;
  margin: 0 0 30rpx 30rpx;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
}

.addb_1 {
  width: 720rpx;
  margin: 20rpx 30rpx 20rpx 30rpx;
  font-size: 32rpx;
  line-height: 40rpx;
  color: #000000;
  height: auto;
  display: block;
  float: left;
}
.addb_2 {
  width: 720rpx;
  margin: 0rpx 30rpx 20rpx 30rpx;
  font-size: 32rpx;
  line-height: 40rpx;
  color: #666666;
  height: auto;
  display: block;
  float: left;
}

.ptt {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}
.pttl {
  width: 84rpx;
  height: 84rpx;
  display: block;
  float: left;
  margin: 18rpx 20rpx 18rpx 30rpx;
}
.pttr {
  width: 400rpx;
  height: 84rpx;
  display: block;
  float: left;
  margin: 18rpx 0rpx 0rpx 0rpx;
}
.pttrt {
  width: 100%;
  height: 42rpx;
  line-height: 42rpx;
  display: block;
  float: left;
  font-size: 34rpx;
  color: #000000;
}
.pttrf {
  width: 100%;
  height: 42rpx;
  line-height: 42rpx;
  display: block;
  float: left;
  font-size: 24rpx;
  color: #999999;
}
.pttjt {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 48rpx 30rpx 0 30rpx;
}
.ptf {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}
.ptft {
  width: 720rpx;
  margin: 0 0 0 30rpx;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
}
.ptft image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 21rpx 20rpx 0 0rpx;
}
.ptft text {
  width: auto;
  height: 48rpx;
  display: block;
  float: left;
  margin: 21rpx 20rpx 0 0rpx;
  font-size: 34rpx;
  line-height: 48rpx;
  color: #000000;
}

.addi2_r {
  width: auto;
  height: 48rpx;
  display: block;
  float: right;
  margin: 21rpx 20rpx 0 0rpx;
  font-size: 34rpx;
  line-height: 48rpx;
  color: #999999;
}
.addi2_r image {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 15rpx 10rpx 0 30rpx;
}

.addi2 {
  width: 720rpx;
  margin: 0 0 0 30rpx;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
  border-bottom: 1px solid #dddddd;
}
.addi2:last-child {
  border: 0;
}
.addi {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  box-shadow: inset 0px 1px 0px #dddddd, inset 0px -1px 0px #dddddd;
  margin: 20rpx 0 0;
}
.addi2 text {
  width: 200rpx;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 34rpx;
  color: #000000;
  margin: 0 0 0 0rpx;
}
.addi text {
  width: 200rpx;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 34rpx;
  color: #000000;
  margin: 0 0 0 30rpx;
}
.addi input,
.addi2 input {
  width: 480rpx;
  height: 50rpx;
  display: block;
  float: left;
  margin: 20rpx 30rpx 0 0;
  text-align: right;
  line-height: 50rpx;
  font-size: 34rpx;
  color: #000000;
}
.addb {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}
.addbi {
  width: 720rpx;
  margin: 0 0 0 30rpx;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
  border-bottom: 1px solid #dddddd;
}
.addb .addbi:last-child {
  border: none;
}
.addbi .addbil {
  width: 200rpx;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 34rpx;
  color: #000000;
}
.addbi .addbir {
  width: auto;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 32rpx;
  color: #000000;
}
.addbi image {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 33rpx 30rpx 0 30rpx;
}
.addi textarea {
  width: 690rpx;
  height: 150rpx;
  display: block;
  float: left;
  margin: 0rpx 30rpx 20rpx;
  text-align: left;
  line-height: 40rpx;
  font-size: 34rpx;
  color: #000000;
}

.index {
  background: rgb(242, 242, 242);
  width: 100%;
  height: auto;
}
.jtop {
  width: 100%;
  height: 88rpx;
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  box-shadow: inset 0px -1px 0px #dddddd;
}
.jtop_l {
  width: auto;
  height: 88rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  color: #000000;
  line-height: 88rpx;
  margin: 0 0 0 30rpx;
}
.jtop_l image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 20rpx 0 20rpx 0;
}
.jtop_lt {
  margin: 0 0 0 10rpx;
}
.jtop_r {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: right;
  margin: 20rpx 20rpx 0 0;
}
.jtop_rl {
  margin: 20rpx 30rpx 0 0;
}
.jtop_r image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
}
.jtop_c {
  width: 70%;
  height: 48rpx;
  line-height: 48rpx;
  display: block;
  float: left;
  position: fixed;
  top: 20rpx;
  left: 15%;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
}
.jtop_c image {
  width: 36rpx;
  height: 36rpx;
  display: inline-block;
  float: none;
  margin-bottom: -5rpx;
  margin-left: 5rpx;
}
.jtop_b {
  width: 100%;
  height: auto;
  background: #ffffff;
  position: fixed;
  top: 88rpx;
  left: 0;
  z-index: 1001;
}
.jtop_r {
  width: auto;
  height: 88rpx;
  display: block;
  float: right;
  margin: 0rpx 20rpx 0 0;
  line-height: 88rpx;
  font-size: 32rpx;
  color: #000000;
  color: #256aff;
}
.doc_b22 {
  opacity: 0.5;
}

.nd {
  width: 735rpx;
  height: auto;
  display: block;
  float: left;
  margin: 0 0 0 0rpx;
}
.nd_bt {
  width: auto;
  height: 48rpx;
  display: block;
  float: right;
  border: 1px solid #dddddd;
  border-radius: 24rpx;
  margin: 40rpx 0 40rpx 30rpx;
}
.nd_bt text {
  width: auto;
  height: 48rpx;
  display: block;
  float: left;
  margin: 0 7rpx 0 18rpx;
  line-height: 48rpx;
  color: #666666;
  font-size: 24rpx;
}
.nd_bt image {
  width: 13rpx;
  height: 8rpx;
  display: block;
  float: left;
  margin: 21rpx 14rpx 0 0;
}

.tc {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
  display: block;
}
.jtop_bj {
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
}
.tc_fbox_bt {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-top: 20rpx solid #f2f2f2;
  background: #ffffff;
}
.tc_fbox {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #f2f2f2;
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 10001;
}
.tc_fbox_bt2 {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-top: 0rpx solid #f2f2f2;
  background: #ffffff;
}
.tc_fbox_bt3 {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-bottom: 1px solid #dddddd;
  background: #ffffff;
}

.qk,
.qr {
  width: 50rpx;
  height: 50rpx;
  display: block;
  float: right;
  margin: 10rpx 40rpx 40rpx 0;
}
</style>
